package com.situ.crm.controller;

import com.situ.crm.dao.IDeptDao;
import com.situ.crm.dao.impl.DeptDaoImpl;
import com.situ.crm.pojo.Dept;
import com.situ.crm.util.JSONUtil;
import com.situ.crm.util.Result;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@WebServlet("/dept")
public class DeptServlet extends HttpServlet {

    private IDeptDao deptDao = new DeptDaoImpl();

    // http://localhost:8088/dept?method=selectAll
    // http://localhost:8088/dept?method=deleteById&id=12
    //访问Servlet默认访问service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决post请求中文乱码问题
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (StringUtils.isEmpty(method)) {
            method = "selectAll";
        }
        switch (method) {
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String idsStr = req.getParameter("ids");// "12,23"
        String[]  ids = idsStr.split(",");// ["12", "23"]
        for (String id : ids) {
            deptDao.deleteById(Integer.parseInt(id));
        }
        resp.sendRedirect("/dept?method=selectAll");
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        Dept dept = new Dept();
        dept.setId(Integer.parseInt(id));
        dept.setName(name);
        dept.setAddress(address);
        dept.setPhone(phone);
        deptDao.update(dept);
        //修改完成之后应该查找对象的数据
        resp.sendRedirect("/dept?method=selectAll");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Dept dept = deptDao.selectById(Integer.parseInt(id));

        req.setAttribute("dept", dept);
        req.getRequestDispatcher("dept_update.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        Dept dept = new Dept();
        dept.setName(name);
        dept.setAddress(address);
        dept.setPhone(phone);

        deptDao.add(dept);

        //添加完成之后应该查找对象的数据
        resp.sendRedirect("/dept?method=selectAll");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        deptDao.deleteById(Integer.parseInt(id));

        //删除完成之后应该查找对象的数据
        resp.sendRedirect("/dept?method=selectAll");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        if (StringUtils.isEmpty(name)) {
            name = "";
        }
        List<Dept> list = deptDao.selectAll(name);

        //把数据放到req这个域对象（可以理解成一块内存区域）
        req.setAttribute("list", list);
        //转发到dept_list.jsp页面进行展示
        req.getRequestDispatcher("dept_list.jsp").forward(req, resp);
    }
}
